﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Sales
{
    public partial class frmManageIndustryType : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstIndustryTypeEntity mSelectIndustryType;
        #endregion

        #region Constructor
        public frmManageIndustryType()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtTypeCode.Text) == false)
            {
                condition += " AND D.INDUSTRY_TYPE_CODE LIKE '%" + txtTypeCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtTypeName.Text) == false)
            {
                condition += " AND D.INDUSTRY_TYPE_NAME LIKE '%" + txtTypeName.Text + "%'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "INDUSTRY_TYPE_CODE";
            DataTable dt = MstIndustryTypeBs.GetIndustryTypeByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectIndustryType = new MstIndustryTypeEntity(dr);
        }

        protected override void Click_View()
        {
            if (mSelectIndustryType == null)
            {
                MessageBox.Show("请选择一行记录！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainIndustryType frm = new frmMaintainIndustryType();
            frm.ThisActionType = ActionType.View;
            frm.SelectIndustryType = mSelectIndustryType;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectIndustryType == null)
            {
                MessageBox.Show("请选择一行记录！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中行业类型？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = MstIndustryTypeBs.DeleteMstIndustryType(mSelectIndustryType);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("此类别已经使用过了，不允许删除！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectIndustryType == null)
            {
                MessageBox.Show("请选择一行记录！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainIndustryType frm = new frmMaintainIndustryType();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectIndustryType = mSelectIndustryType;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainIndustryType frm = new frmMaintainIndustryType();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }
        #endregion

    }
}
